package game;

import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public abstract class AbstractRunnable
{
    private boolean               running;
    private boolean               paused;
    private String                status         = "STOP";
    
    protected final static String STATUS_RUNNING = "RUNNING";
    protected final static String STATUS_STOPPED = "STOP";
    protected final static String STATUS_PAUSED  = "PAUSE";
    
    public boolean isRunning()
    {
        return running;
    }
    
    public boolean isPaused()
    {
        return paused;
    }
    
    protected void start()
    {
        running = true;
        paused = false;
        status = STATUS_RUNNING;
        requestRestart = false;
    }
    
    private void stopped()
    {
        running = false;
        paused = false;
        status = STATUS_STOPPED;
        requestStop = false;
    }
    
    protected final void setStatus(String status)
    {
        this.status = status;
    }
    
    protected String getStatus()
    {
        return status;
    }
    
    public void pause()
    {
        if (isRunning())
        {
            if (isPaused() == false)
            {
                paused = true;
                status = STATUS_PAUSED;
            }
        }
        else
        {
            Game.log("CAN NOT PAUSE, NOT RUNNING");
        }
    }
    
    protected void resume()
    {
        if (isRunning() && isPaused())
        {
            paused = false;
            status = STATUS_RUNNING;
        }
        else
        {
            Game.log("CAN NOT RESUME, RUNNING = '" + isRunning() + "', PAUSED = '" + isPaused() + "'");
        }
    }
    
    private boolean requestRestart = false;
    private boolean requestStop    = false;
    
    private boolean isRequestRestart()
    {
        return requestRestart;
    }
    
    private boolean isRequestStop()
    {
        return requestStop;
    }
    
    protected void requestRestart()
    {
        requestRestart = true;
    }
    
    protected void requestStop()
    {
        requestStop = true;
        Game.log("REQUEST STOP");
    }
    
    protected abstract void restart();
    
    protected abstract void doNext();
    
    public final void update()
    {
        if (isRequestStop())
        {
            stopped();
        }
        else if (isRequestRestart())
        {
            restart();
        }
        else
        {
            if (isRunning() && isPaused() == false)
            {
                doNext();
            }
        }
    }
    
    public KeyListener getKeyboardControl()
    {
        return null;
    }

    public MouseListener getMouseControl()
    {
        return null;
    }

    public MouseMotionListener getMouseMotionListener()
    {
        return null;
    }
}